﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using kim.DBUtility;//Please add references
namespace kim.DAL
{
	/// <summary>
	/// 数据访问类:link
	/// </summary>
	public partial class link
	{
		public link()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return kim.DBUtility.DbHelperSQL.GetMaxID("id", "link",""); 
		}
        public DataSet GetNewsPager(string tbname, string FieldKey, int PageCurrent,
int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            SqlParameter[] parameters ={
               new SqlParameter("@tbname",SqlDbType.VarChar,128),
               new SqlParameter("@FieldKey",SqlDbType.VarChar,1000),
               new SqlParameter("@PageCurrent",SqlDbType.Int),
               new SqlParameter("@PageSize",SqlDbType.Int),
               new SqlParameter("@FieldShow",SqlDbType.NVarChar,1000),
               new SqlParameter("@FieldOrder",SqlDbType.NVarChar,1000),
               new SqlParameter("@Where",SqlDbType.NVarChar,1000),
               new SqlParameter("@ItemCount",SqlDbType.Int),
                new SqlParameter("@PageCount",SqlDbType.Int)};

            parameters[0].Value = tbname;
            parameters[1].Value = FieldKey;
            parameters[2].Value = PageCurrent;
            parameters[3].Value = PageSize;
            parameters[4].Value = FieldShow;
            parameters[5].Value = FieldOrder;
            parameters[6].Value = Where;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            return Common.SqlDbHelper.GetDataSetProc("sp_PageView", parameters);
        }
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from link");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return kim.DBUtility.DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(kim.Model.link model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into link(");
			strSql.Append("title,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time,typeid,ChannelID)");
			strSql.Append(" values (");
			strSql.Append("@title,@site_url,@img_url,@is_image,@sort_id,@is_red,@is_lock,@add_time,@typeid,@ChannelID)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.NVarChar,255),
					new SqlParameter("@site_url", SqlDbType.NVarChar,255),
					new SqlParameter("@img_url", SqlDbType.NVarChar,255),
					new SqlParameter("@is_image", SqlDbType.Int,4),
					new SqlParameter("@sort_id", SqlDbType.Int,4),
					new SqlParameter("@is_red", SqlDbType.TinyInt,1),
					new SqlParameter("@is_lock", SqlDbType.TinyInt,1),
					new SqlParameter("@add_time", SqlDbType.DateTime),
					new SqlParameter("@typeid", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.site_url;
			parameters[2].Value = model.img_url;
			parameters[3].Value = model.is_image;
			parameters[4].Value = model.sort_id;
			parameters[5].Value = model.is_red;
			parameters[6].Value = model.is_lock;
			parameters[7].Value = model.add_time;
			parameters[8].Value = model.typeid;
			parameters[9].Value = model.ChannelID;

			object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(kim.Model.link model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update link set ");
			strSql.Append("title=@title,");
			strSql.Append("site_url=@site_url,");
			strSql.Append("img_url=@img_url,");
			strSql.Append("is_image=@is_image,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("is_red=@is_red,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("add_time=@add_time,");
			strSql.Append("typeid=@typeid,");
			strSql.Append("ChannelID=@ChannelID");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.NVarChar,255),
					new SqlParameter("@site_url", SqlDbType.NVarChar,255),
					new SqlParameter("@img_url", SqlDbType.NVarChar,255),
					new SqlParameter("@is_image", SqlDbType.Int,4),
					new SqlParameter("@sort_id", SqlDbType.Int,4),
					new SqlParameter("@is_red", SqlDbType.TinyInt,1),
					new SqlParameter("@is_lock", SqlDbType.TinyInt,1),
					new SqlParameter("@add_time", SqlDbType.DateTime),
					new SqlParameter("@typeid", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.site_url;
			parameters[2].Value = model.img_url;
			parameters[3].Value = model.is_image;
			parameters[4].Value = model.sort_id;
			parameters[5].Value = model.is_red;
			parameters[6].Value = model.is_lock;
			parameters[7].Value = model.add_time;
			parameters[8].Value = model.typeid;
			parameters[9].Value = model.ChannelID;
			parameters[10].Value = model.id;

			int rows=kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from link ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from link ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public kim.Model.link GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,title,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time,typeid,ChannelID from link ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			kim.Model.link model=new kim.Model.link();
			DataSet ds=kim.DBUtility.DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["title"]!=null && ds.Tables[0].Rows[0]["title"].ToString()!="")
				{
					model.title=ds.Tables[0].Rows[0]["title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["site_url"]!=null && ds.Tables[0].Rows[0]["site_url"].ToString()!="")
				{
					model.site_url=ds.Tables[0].Rows[0]["site_url"].ToString();
				}
				if(ds.Tables[0].Rows[0]["img_url"]!=null && ds.Tables[0].Rows[0]["img_url"].ToString()!="")
				{
					model.img_url=ds.Tables[0].Rows[0]["img_url"].ToString();
				}
				if(ds.Tables[0].Rows[0]["is_image"]!=null && ds.Tables[0].Rows[0]["is_image"].ToString()!="")
				{
					model.is_image=int.Parse(ds.Tables[0].Rows[0]["is_image"].ToString());
				}
				if(ds.Tables[0].Rows[0]["sort_id"]!=null && ds.Tables[0].Rows[0]["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_red"]!=null && ds.Tables[0].Rows[0]["is_red"].ToString()!="")
				{
					model.is_red=int.Parse(ds.Tables[0].Rows[0]["is_red"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_lock"]!=null && ds.Tables[0].Rows[0]["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
				}
				if(ds.Tables[0].Rows[0]["add_time"]!=null && ds.Tables[0].Rows[0]["add_time"].ToString()!="")
				{
					model.add_time=DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString());
				}
				if(ds.Tables[0].Rows[0]["typeid"]!=null && ds.Tables[0].Rows[0]["typeid"].ToString()!="")
				{
					model.typeid=int.Parse(ds.Tables[0].Rows[0]["typeid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ChannelID"]!=null && ds.Tables[0].Rows[0]["ChannelID"].ToString()!="")
				{
					model.ChannelID=int.Parse(ds.Tables[0].Rows[0]["ChannelID"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time,typeid,ChannelID ");
			strSql.Append(" FROM link ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,title,site_url,img_url,is_image,sort_id,is_red,is_lock,add_time,typeid,ChannelID ");
			strSql.Append(" FROM link ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM link ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from link T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "link";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return kim.DBUtility.DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

